package com.up.vms.application.common;


import org.springframework.data.domain.Page;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangshd on 2019/3/12.
 */
public class DomainUtils {
    /**
     * 对象转换,将page对象转化为传递业务数据的通用值对象
     *
     * @param page
     * @return geneRespDTO
     * @throws Exception
     */
    public static GeneRespDTO<Map<String, Object>> convert(Page page) {
        GeneRespDTO<Map<String, Object>> geneRespDTO = null;
        if (CommonUtils.isNotNull(page)) {
            geneRespDTO = new GeneRespDTO();
            HashMap<String, Object> map = new HashMap();
            map.put("content", page.getContent());
            map.put("currentPage", page.getNumber());
            map.put("totalPages", page.getTotalPages());
            map.put("totalRecords", page.getTotalElements());

            geneRespDTO.setData(map);
            geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        }
        return geneRespDTO;
    }
}
